class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    vector<vector<int>> permute(vector<int>& nums) {
        vector<bool> check(nums.size(), false);
        dfs(nums, check, 0);
        return ans;
    }

    void dfs(vector<int>& nums, vector<bool>& check, int pos)
    {
        if (path.size() == nums.size())
        {
            ans.push_back(path);
            return;
        }

        for (int i = 0; i < nums.size(); ++i)
        {
            if (!check[i])
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums, check, pos + 1);
                check[i] = false;
                path.pop_back();
            }

        }
    }
};